﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Data;
using Pub.Lib;
using Pub.Tools;


namespace Pub.Controls
{

    public delegate void NewRowHandler(object sender, DataTableNewRowEventArgs e);
    public delegate void Row_ChangingHandler(object sender, DataRowChangeEventArgs e);
    public delegate void Row_ChangedHandler(object sender, DataRowChangeEventArgs e);
    public delegate void Column_ChangingHandler(object sender, DataColumnChangeEventArgs e);
    public delegate void Row_DeletingHandler(object sender, DataRowChangeEventArgs e);
    public delegate void Row_DeletedHandler(object sender, DataRowChangeEventArgs e);


    public partial class DataTableEx : Component
    {
        [
         Category("专用设置"),
         Description("新增行,例:e.Row[\"ID\"] = 10;")
         ]
        public event NewRowHandler OnNewRow = null;
        public event Row_ChangingHandler OnRow_Changing = null;
        public event Row_ChangedHandler OnRow_Changed = null;
        public event Column_ChangingHandler OnColumn_Changing = null;

        [
         Category("专用设置"),
         Description("DataRow 已标记为 Deleted 前发生")
         ]
        public event Row_DeletingHandler OnRow_Deleting = null;

        [
         Category("专用设置"),
         Description("DataRow 已标记为 Deleted 后发生")
         ]
        public event Row_DeletedHandler OnRow_Deleted = null;
        

        public DataTableEx()
        {
            InitializeComponent();
        }

        public DataTableEx(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        private DataTable _DT = null;
        private string _Text = string.Empty;
        private string _Conn = string.Empty;
        private string _UpdateTable = string.Empty;

       

        public virtual void RowNew(object sender)
        { 
        }


        [
         Category("专用设置"),
         Description("设置DataTable中SQL语句")
         ]
        public string Text
        {
            set
            {
                _Text = value;
            }
            get
            {
                return _Text;
            }
        }

        [
         Category("专用设置"),
         Description("设置DataTable中SQL语句")
         ]
        public string[] Lines
        {
            set
            {
                _Text = string.Join("\r\n", value, 0, value.Length);

            }
            get
            {
                return System.Text.RegularExpressions.Regex.Split(_Text, @"[\r\n]+");
            }
        }


        [
         Category("专用设置"),
         Description("设置SQL语句中\"连接\"")
         ]
        public string Conn
        {
            set
            {
                _Conn = value;
            }
            get
            {
                if (_Conn == string.Empty)
                {
                    return PubVar.ConnMain;
                }
                else
                {
                    return _Conn;
                }
            }
        }

        [
         Category("专用设置"),
         Description("返回的DataTable")
         ]
        public DataTable DataTable
        {
            get
            {
                if (_DT == null)
                {
                    DataBinds();
                }
                return _DT;
            }
        }

        [
         Category("专用设置"),
         Description("更新表名称")
         ]
        public string UpdateTable
        {
            set
            {
                _UpdateTable = value;
            }

            get
            {
                if ((_UpdateTable == string.Empty) && (_Text != string.Empty))
                {
                    _UpdateTable = DBClass.GetDBName(_Text);
                }
                return _UpdateTable;
            }
        }


        [
         Category("专用设置"),
         Description("保存DataTable")
         ]
       public Boolean Save
        {
            get
            {
                Boolean bl = true;
                if (_DT != null)
                {
                    if ((_DT.GetChanges() != null) && (UpdateTable != string.Empty))
                    {
                        bl = Helper.DBHelper.SaveDataTable(_Text, _DT.GetChanges(), Conn, UpdateTable);
                    }
                }
                return bl;
            }
        }

        public void DataBinds()
        {
            if (_Text != string.Empty)
            {
                _DT = Helper.DBHelper.GetDataTable(_Text, _Conn);
                if (_DT != null)
                {
                    _DT.TableNewRow += new DataTableNewRowEventHandler(Table_NewRow);
                    _DT.RowChanging += new DataRowChangeEventHandler(Row_Changing);
                    _DT.ColumnChanging += new DataColumnChangeEventHandler(Column_Changing);
                    _DT.RowDeleting += new DataRowChangeEventHandler(Row_Deleting);
                    _DT.RowDeleted += new DataRowChangeEventHandler(Row_Deleted);
                    _DT.RowChanged += new DataRowChangeEventHandler(Row_Changed);
                }
            }
        }

 


        private void Table_NewRow(object sender, DataTableNewRowEventArgs e)
        {
            if (OnNewRow != null) OnNewRow(sender, e);
        }

        private void Row_Changing(object sender, DataRowChangeEventArgs e)
        {
            if (OnRow_Changing != null) OnRow_Changing(sender, e);
            
        }
        private void Column_Changing(object sender, DataColumnChangeEventArgs e)
        {
            if (OnColumn_Changing != null) OnColumn_Changing(sender, e);
        }

        private void Row_Deleting(object sender, DataRowChangeEventArgs e)
        {
            if (OnRow_Deleting != null) OnRow_Deleting(sender, e);
        }

        private void Row_Deleted(object sender, DataRowChangeEventArgs e)
        {
           if (OnRow_Deleted != null) OnRow_Deleted(sender, e);
        }

        private  void Row_Changed(object sender, DataRowChangeEventArgs e)
        {
            if (OnRow_Changed != null) OnRow_Changed(sender, e);            
        }
    }
}
